package com.jcrj.service;

import com.jcrj.dao.TemplateDao;
import com.jcrj.entity.Bookmarks;
import com.jcrj.entity.OSignLimit;
import com.jcrj.entity.Signature;
import com.jcrj.entity.TemplateFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TemplateService {
	private TemplateDao templateDao;

	public TemplateDao getTemplateDao() {
		return this.templateDao;
	}

	public void setTemplateDao(TemplateDao templateDao) {
		this.templateDao = templateDao;
	}

	public void saveBookMark(Bookmarks bookmarks) {
		this.templateDao.saveBookMark(bookmarks);
	}

	public void updateBookMark(Bookmarks bookmarks) {
		this.templateDao.updateBookMark(bookmarks);
	}

	public Bookmarks getBookMark(int id) {
		return (Bookmarks) this.templateDao.getObject(new Bookmarks(), id);
	}

	public List<Bookmarks> findBookMarkList() {
		String hql = "from Bookmarks  order by bookmarkid desc";
		return this.templateDao.findByHql(hql);
	}

	public void deleteBookMark(String[] ids) {
		if (ids != null) {
			String id = "";
			for (int i = 0; i < ids.length; i++) {
				if (id == "")
					id = id + ids[i];
				else {
					id = id + "," + ids[i];
				}
			}
			String hql = "delete from Bookmarks where bookmarkid in(" + id + ")";
			this.templateDao.executeHql(hql);
		}
	}

	public List findTemplateList(int currentPage) {
		String hql = "select recordid,filename,descript,userId from  TemplateFile order by recordid desc";
		List<Object[]> list = this.templateDao.findByPage(hql, currentPage);
		List fileList = new ArrayList();
		for (Object[] obj : list) {
			TemplateFile file = new TemplateFile();
			file.setRecordid(obj[0].toString());
			file.setFilename(obj[1].toString());
			file.setDescript(obj[2].toString());
			if ((obj[3] != null) && (!"".equals(obj[3]))) {
				file.setUserId(Integer.valueOf(Integer.parseInt(obj[3].toString())));
			}
			fileList.add(file);
		}
		return fileList;
	}

	public Long countTemplate() {
		String hql = "select count(recordid) from  TemplateFile";
		return this.templateDao.countTotalByHql(hql);
	}

	public List<TemplateFile> findTemplateList() {
		String hql = "select recordid,filename,descript from  TemplateFile order by recordid desc";
		@SuppressWarnings("unchecked")
		List<Object[]> list = this.templateDao.findByHql(hql);
		List<TemplateFile> fileList = new ArrayList<TemplateFile>();
		for (Object[] obj : list) {
			TemplateFile file = new TemplateFile();
			file.setRecordid(obj[0].toString());
			file.setFilename(obj[1].toString());
			file.setDescript(obj[2].toString());
			fileList.add(file);
		}
		return fileList;
	}

	public void updateTemplate(TemplateFile file) {
		this.templateDao.updateTemplate(file);
	}

	public void updateTemplateByName(String mFileName, String mDescript, String mRecordID, String userId) {
		String mSql = "Update Template_File Set FileName = '" + mFileName + "',Descript = '" + mDescript + "',user_id=" + userId + " Where RecordID='"
				+ mRecordID + "'";
		this.templateDao.executeSql(mSql);
	}

	public TemplateFile getTemplateFileById(String recordId) {
		return (TemplateFile) this.templateDao.getObject(new TemplateFile(), recordId);
	}

	public List findListByhql(String hql) {
		return this.templateDao.findByHql(hql);
	}

	public List findListBySql(String sql) {
		return this.templateDao.findBySql(sql);
	}

	public void deleteTemplate(String[] ids) {
		if (ids != null) {
			String id = "";
			for (int i = 0; i < ids.length; i++) {
				if (id == "")
					id = id + ids[i];
				else {
					id = id + "," + ids[i];
				}
			}
			String hql = "delete from TemplateFile where id in(" + id + ")";
			this.templateDao.executeHql(hql);
		}
	}

	public List<Signature> findSignatureList(String name, int currentPage) {
		String hql = "select signatureid,username,markname,marktype,markdate,userId,px from Signature ";
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " where markname like '%" + name.trim() + "%'";
		}
		hql = hql + " order by px,markdate desc";
		List<Object[]> list = this.templateDao.findByPage(hql, currentPage);
		List signList = new ArrayList();
		for (Object[] obj : list) {
			Signature sign = new Signature();
			sign.setSignatureid(Integer.valueOf(Integer.parseInt(obj[0].toString())));
			sign.setUsername(getToString(obj[1]));
			sign.setMarkname(getToString(obj[2]));
			sign.setMarktype(getToString(obj[3]));
			sign.setMarkdate((Date) obj[4]);
			sign.setUserId(Integer.valueOf(Integer.parseInt(obj[5].toString())));
			sign.setPx(isEmpty(obj[6]));
			signList.add(sign);
		}
		return signList;
	}

	private Integer isEmpty(Object obj) {
		if ((obj != null) && (!"".equals(obj))) {
			return Integer.valueOf(Integer.parseInt(obj.toString()));
		}
		return null;
	}

	public Long countSignature(String name) {
		String hql = "select count(signatureid) from Signature ";
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " where markname like '%" + name.trim() + "%'";
		}
		return this.templateDao.countTotalByHql(hql);
	}

	private String getToString(Object obj) {
		if ((obj != null) && (!"".equals(obj))) {
			return obj.toString();
		}
		return "";
	}

	public void saveSignature(Signature signature, String orgs, String names, String users, String userNames) {
		this.templateDao.saveSignature(signature);
		if ((orgs != null) && (!"".equals(orgs))) {
			String[] orgIds = orgs.split(",");
			String[] orgNames = names.split(",");
			for (int i = 0; i < orgIds.length; i++) {
				OSignLimit limit = new OSignLimit();
				limit.setIds(Integer.valueOf(Integer.parseInt(orgIds[i])));
				limit.setNames(orgNames[i]);
				limit.setType("org");
				limit.setSignId(signature.getSignatureid());
				this.templateDao.saveSignLimit(limit);
			}
		}
		if ((users != null) && (!"".equals(users))) {
			String[] userIds = users.split(",");
			String[] userNs = userNames.split(",");
			for (int i = 0; i < userIds.length; i++) {
				OSignLimit limit = new OSignLimit();
				limit.setIds(Integer.valueOf(Integer.parseInt(userIds[i])));
				limit.setNames(userNs[i]);
				limit.setType("user");
				limit.setSignId(signature.getSignatureid());
				this.templateDao.saveSignLimit(limit);
			}
		}
	}

	public Signature getSignatureById(Integer id) {
		return (Signature) this.templateDao.getObject(new Signature(), id.intValue());
	}

	public List<OSignLimit> getLimitBySignId(Integer id) {
		String hql = "from OSignLimit where signId=" + id;
		return this.templateDao.findByHql(hql);
	}

	public void updateSignature(Signature signature) {
		this.templateDao.updateSignature(signature);
	}

	public void updateSignature(Signature signature, String orgs, String names, String users, String userNames) {
		this.templateDao.updateSignature(signature);
		String hql = "delete from OSignLimit where signId=" + signature.getSignatureid();
		this.templateDao.executeHql(hql);
		if ((orgs != null) && (!"".equals(orgs))) {
			String[] orgIds = orgs.split(",");
			String[] orgNames = names.split(",");
			for (int i = 0; i < orgIds.length; i++) {
				OSignLimit limit = new OSignLimit();
				limit.setIds(Integer.valueOf(Integer.parseInt(orgIds[i])));
				limit.setNames(orgNames[i]);
				limit.setType("org");
				limit.setSignId(signature.getSignatureid());
				this.templateDao.saveSignLimit(limit);
			}
		}
		if ((users != null) && (!"".equals(users))) {
			String[] userIds = users.split(",");
			String[] userNs = userNames.split(",");
			for (int i = 0; i < userIds.length; i++) {
				OSignLimit limit = new OSignLimit();
				limit.setIds(Integer.valueOf(Integer.parseInt(userIds[i])));
				limit.setNames(userNs[i]);
				limit.setType("user");
				limit.setSignId(signature.getSignatureid());
				this.templateDao.saveSignLimit(limit);
			}
		}
	}

	public Signature getSignatureObj(int id) {
		return (Signature) this.templateDao.getObject(new Signature(), id);
	}

	public void deleteSignature(String[] ids) {
		if (ids != null) {
			String id = "";
			for (int i = 0; i < ids.length; i++) {
				if (id == "")
					id = id + ids[i];
				else {
					id = id + "," + ids[i];
				}
			}
			String hql = "delete from Signature where signatureid in(" + id + ")";
			this.templateDao.executeHql(hql);
		}
	}
}